package com.sample.wanandroidx.app;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.azhon.appupdate.utils.LogUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.king.zxing.CaptureActivity;
import com.luck.picture.lib.PictureBaseActivity;
import com.sample.wanandroidx.R;
import com.yalantis.ucrop.UCropActivity;

import org.jetbrains.annotations.NotNull;

import java.util.Stack;


/**
 * @author ddnosh
 * @website http://blog.csdn.net/ddnosh
 */
public class AQActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

    public static Stack<Activity> store = new Stack<>();

    private static final String TAG = "AQActivityLifecycleCallbacks";

    private Activity visibleActivity = null;

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        LogUtil.i(TAG, activity.getLocalClassName() + " was Created, " + "activity==null    " + ", activity.isFinishing()    " + activity.isFinishing() + ", activity.isDestroyed()    " + activity.isDestroyed());


        store.add(activity);

        if (activity instanceof PictureBaseActivity || activity instanceof UCropActivity) {
            return;
        }
        if (activity instanceof CaptureActivity) {
            ImmersionBar.with(activity).navigationBarColor(R.color.white).navigationBarDarkIcon(true).fitsSystemWindows(false).statusBarDarkFont(true).init();
            return;
        }
        ImmersionBar.with(activity).navigationBarColor(R.color.white).navigationBarDarkIcon(true).fitsSystemWindows(true).statusBarDarkFont(true).init();
    }

    @Override
    public void onActivityStarted(Activity activity) {
        LogUtil.i(TAG, activity.getLocalClassName() + " was Started, " + "activity==null    " + ", activity.isFinishing()    " + activity.isFinishing() + ", activity.isDestroyed()    " + activity.isDestroyed());
    }

    @Override
    public void onActivityResumed(Activity activity) {
        LogUtil.i(TAG, activity.getLocalClassName() + " was oResumed, " + "activity==null   " + ", activity.isFinishing()    " + activity.isFinishing() + ", activity.isDestroyed()    " + activity.isDestroyed());
        visibleActivity = activity;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        LogUtil.i(TAG, activity.getLocalClassName() + " was Pauseed, " + "activity==null  " + ", activity.isFinishing()    " + activity.isFinishing() + ", activity.isDestroyed()    " + activity.isDestroyed());
        visibleActivity = null;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        LogUtil.i(TAG, activity.getLocalClassName() + " was Stoped, " + "activity==null   " + ", activity.isFinishing()    " + activity.isFinishing() + ", activity.isDestroyed()    " + activity.isDestroyed());
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, @NotNull Bundle outState) {
        LogUtil.i(TAG, activity.getLocalClassName() + " was SaveInstanceState, " + "activity==null    " + ", activity.isFinishing()    " + activity.isFinishing() + ", activity.isDestroyed()    " + activity.isDestroyed());
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        LogUtil.i(TAG, activity.getLocalClassName() + " was Destroyed, " + "activity==null  " + ", activity.isFinishing()    " + activity.isFinishing() + ", activity.isDestroyed()    " + activity.isDestroyed());
        store.remove(activity);
    }

    public Activity getCurrentVisibleActivity() {
        return visibleActivity;
    }

    public void finishAll() {
        for (Activity activity : store) {
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public boolean isInForeground() {
        return store.size() > 0;
    }
}
